<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="###Java Annotation注解、  J2SE5.0版本在 java.lang.annotation提供了四种元注解，专门注解其他的注解：  @Documented –注解是否将包含在JavaDoc中@Retention –什么时候使用该注解@Target –注解用于什么地方@Inherited – 是否允许子类继承该注解">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Java-Annotation注解">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Java/Java-Annotation注解/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="###Java Annotation注解、  J2SE5.0版本在 java.lang.annotation提供了四种元注解，专门注解其他的注解：  @Documented –注解是否将包含在JavaDoc中@Retention –什么时候使用该注解@Target –注解用于什么地方@Inherited – 是否允许子类继承该注解">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-1534fcb96a5c1cb1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-732f5d3b1ed36bbd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-402ccd6dc6a3f950.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-8ecc02bab563240f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-09645a21c0fdad6b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T10:45:24.675Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java-Annotation注解">
<meta name="twitter:description" content="###Java Annotation注解、  J2SE5.0版本在 java.lang.annotation提供了四种元注解，专门注解其他的注解：  @Documented –注解是否将包含在JavaDoc中@Retention –什么时候使用该注解@Target –注解用于什么地方@Inherited – 是否允许子类继承该注解">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/3110861-1534fcb96a5c1cb1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Java/Java-Annotation注解/"/>





  <title>Java-Annotation注解 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Java/Java-Annotation注解/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Java-Annotation注解</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>###Java Annotation注解、</p>
<ol>
<li>J2SE5.0版本在 java.lang.annotation提供了四种元注解，专门注解其他的注解：</li>
</ol>
<p>@Documented –注解是否将包含在JavaDoc中<br>@Retention –什么时候使用该注解<br>@Target –注解用于什么地方<br>@Inherited – 是否允许子类继承该注解<br><a id="more"></a></p>
<ol start="2">
<li>@Documented–一个简单的Annotations标记注解，表示是否将注解信息添加在java文档中。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">* 元注解被@Documented修饰，表示它本身也会被文档化。@Retention元注解的值</span><br><span class="line">* RetentionPolicy.RUNTIME表示@Documented这个注解能保留到运行时</span><br><span class="line">* @Target元注解的值ElementType.ANNOTATION_TYPE表示@Documented这个注解只能够用来描述注解类型。</span><br><span class="line">*/</span><br><span class="line">@Documented</span><br><span class="line">@Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">@Target(ElementType.ANNOTATION_TYPE)</span><br><span class="line">public @interface Documented &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>示例：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line"></span><br><span class="line">public class DocumentAnnotation &#123;</span><br><span class="line">    @Documented</span><br><span class="line">    @Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">    @Target(ElementType.TYPE)</span><br><span class="line">    public @interface DocumentA&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    @Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">    @Target(ElementType.TYPE)</span><br><span class="line">    public @interface DocumentB&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @DocumentA</span><br><span class="line">    public class TestDocumentA&#123;</span><br><span class="line">        public void a()&#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @DocumentB</span><br><span class="line">    public class TestDocumentB&#123;</span><br><span class="line">        public void b()&#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>使用命令生成JavaDoc文档。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javadoc xxx.java</span><br></pre></td></tr></table></figure></p>
<p>效果图如下：<br><img src="http://upload-images.jianshu.io/upload_images/3110861-1534fcb96a5c1cb1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图1.png"></p>
<ol start="3">
<li>@Retention– 定义该注解的生命周期。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">public @interface Developer &#123;</span><br><span class="line">    String value();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>RetentionPolicy.SOURCE – 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义，所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。<br>RetentionPolicy.CLASS – 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式。<br>RetentionPolicy.RUNTIME– 始终不会丢弃，运行期也保留该注解，因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。</p>
<ol start="4">
<li><p>@Target – 表示该注解用于什么地方。如果不明确指出，该注解可以放在任何地方。以下是一些可用的参数。需要说明的是：属性的注解是兼容的，如果你想给7个属性都添加注解，仅仅排除一个属性，那么你需要在定义target包含所有的属性。<br>ElementType是一个枚举类型，它可以取以下值：<br>TYPE：表示可以用来注解类、接口、注解类型或枚举类型；<br>PACKAGE：可以用来注解包；<br>PARAMETER：可以用来注解参数；<br>ANNOTATION_TYPE：可以用来注解 注解类型；<br>METHOD：可以用来注解方法；<br>FIELD：可以用来注解属性（包括枚举常量）；<br>CONSTRUCTOR：可以用来注解构造器；<br>LOCAL_VARIABLE：可用来注解局部变量。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@Documented</span><br><span class="line">@Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">@Target(ElementType.ANNOTATION_TYPE)</span><br><span class="line">public @interface Target &#123;</span><br><span class="line">    ElementType[] value();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>@Inherited – 定义该注释和子类的关系<br>表明被修饰的注解类型是自动继承的。具体解释如下：若一个注解类型被Inherited元注解所修饰，则当用户在一个类声明中查询该注解类型时，若发现这个类声明中不包含这个注解类型，则会自动在这个类的父类中查询相应的注解类型，这个过程会被重复，直到该注解类型被找到或是查找完了Object类还未找到。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@Documented</span><br><span class="line">@Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">@Target(ElementType.ANNOTATION_TYPE)</span><br><span class="line">public @interface Inherited &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p><strong>如果注解中只有一个属性，可以直接命名为“value”，使用时无需再标明属性名。</strong><br>示例：<br>可以让注解被继承，但这并不是真的继承，只是通过使用@Inherited，可以让子类Class对象使用getAnnotations()获取父类被@Inherited修饰的注解<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">public class InheritedAnnotation &#123;</span><br><span class="line">    @Inherited</span><br><span class="line">    @Target(ElementType.TYPE)</span><br><span class="line">    @Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">    public @interface DocumentA &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Target(ElementType.TYPE)</span><br><span class="line">    @Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">    public @interface DocumentB &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @InheritedAnnotation.DocumentA</span><br><span class="line">    class A&#123;&#125;</span><br><span class="line"></span><br><span class="line">    class B extends A&#123;&#125;</span><br><span class="line"></span><br><span class="line">    @InheritedAnnotation.DocumentB</span><br><span class="line">    class C&#123;&#125;</span><br><span class="line"></span><br><span class="line">    class D extends C&#123;&#125;</span><br><span class="line"></span><br><span class="line">    public void main()&#123;</span><br><span class="line">        A a = new B();</span><br><span class="line">        System.out.println(&quot;已使用的@Inherited注解:&quot;+ Arrays.toString(a.getClass().getAnnotations()));</span><br><span class="line"></span><br><span class="line">        C c = new D();</span><br><span class="line">        System.out.println(&quot;没有使用的@Inherited注解:&quot;+Arrays.toString(c.getClass().getAnnotations()));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-732f5d3b1ed36bbd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图2.png"></p>
<ol start="6">
<li>自定义注解<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">@Documented</span><br><span class="line">@Retention(RetentionPolicy.CLASS)</span><br><span class="line">@Target(ElementType.METHOD)</span><br><span class="line">@Inherited</span><br><span class="line">public @interface MethodInfo &#123;</span><br><span class="line">	/**</span><br><span class="line">	 * 在自定义注解时，有以下几点需要我们了解：</span><br><span class="line">	 * 1. 注解类型是通过&quot;@interface&quot;关键字定义的;</span><br><span class="line">	 * 2. 在&quot;注解体&quot;中，所有的方法均没有方法体且只允许public和abstract这两种</span><br><span class="line">	 * 		修饰符号（不加修饰符缺省为public），注解方法不允许有throws子句；</span><br><span class="line">	 * 3. 注解方法的返回值只能为以下几种：原始数据类型）, String, Class, 枚举类型,</span><br><span class="line">	 * 	     注解和它们的一维数组，可以为方法指定默认返回值。</span><br><span class="line">	 */</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	String author() default &quot;mazaiting&quot;;</span><br><span class="line">	String date();</span><br><span class="line">	int version() default 1;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">public class Client &#123;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		exec();</span><br><span class="line">		exec1();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@MethodInfo(date = &quot;2017.8.29&quot; )</span><br><span class="line">	private static void exec() &#123;</span><br><span class="line">		System.out.println(&quot;exec&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@MethodInfo(author = &quot;zaitingma&quot;,date = &quot;2017.8.29&quot;,version=2)</span><br><span class="line">	private static void exec1() &#123;</span><br><span class="line">		System.out.println(&quot;exec1&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时的注解还未曾起任何作用，接下来进行注解解析，解析后，注解就会起相应的作用。</p>
<ol start="7">
<li>注解的解析–编译时解析/运行时解析</li>
</ol>
<p><strong>编译时解析</strong><br>编译时注解指的是@Retention的值为CLASS的注解，对于这类注解的解析，我们只需做以下两件事：<br>自定义类继承 AbstractProcessor类；<br>重写其中的 process 函数。<br>编译器在编译时会自动查找所有继承自 AbstractProcessor 的类，然后调用他们的 process 方法。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">1. 新建注解类</span><br><span class="line">@Target(&#123; ElementType.FIELD, ElementType.TYPE &#125;)  </span><br><span class="line">@Retention(RetentionPolicy.CLASS)  </span><br><span class="line">public @interface Seriable &#123;  </span><br><span class="line">      </span><br><span class="line">&#125; </span><br><span class="line">2. 新建BeanProcessor继承自AbstractProcessor类，类上必须加上注解@SupportedAnnotationTypes，括号中的字符串为新注解的包名+类名。</span><br><span class="line">@SupportedAnnotationTypes(&quot;com.mazaiting.Seriable&quot;)</span><br><span class="line">@SupportedSourceVersion(SourceVersion.RELEASE_8)</span><br><span class="line">public class BeanProcessor extends AbstractProcessor &#123;</span><br><span class="line">	@Override</span><br><span class="line">	public boolean process(Set&lt;? extends TypeElement&gt; annotations, RoundEnvironment roundEnv) &#123;</span><br><span class="line">		try &#123;</span><br><span class="line">			File file = new File(&quot;F:\\test.txt&quot;);</span><br><span class="line">			FileOutputStream fos = new FileOutputStream(file);</span><br><span class="line">			fos.write(&quot;wohenhao&quot;.getBytes());</span><br><span class="line">			fos.close();</span><br><span class="line">		&#125; catch (Exception e) &#123;</span><br><span class="line">			e.printStackTrace();</span><br><span class="line">		&#125;</span><br><span class="line">		return true;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">在工程目录下新建resources/META-INF/services,然后新建文件javax.annotation.processing.Processor，里面写入BeanProcessor类的全路径名。如图2.</span><br><span class="line">3. 导出为jar包</span><br><span class="line">右键工程-&gt;export-&gt;JAR file-&gt;finish.</span><br><span class="line">4. 新建测试工程</span><br><span class="line">将上一步导出的jar包引入新工程，新建主类</span><br><span class="line">public class Client &#123;</span><br><span class="line">	@Seriable</span><br><span class="line">	String cString;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		System.out.println(1);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">5. 开启Factory Path</span><br><span class="line">右键工程-&gt;Java Compiler-&gt;Annotation Processing-&gt;Factory Path-&gt;Add JARs，选择刚刚导入的jar包，点击OK。</span><br><span class="line">6. 右键运行项目，在F盘下即可看到test.txt文件。</span><br></pre></td></tr></table></figure></p>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-402ccd6dc6a3f950.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图3.png"><br><img src="http://upload-images.jianshu.io/upload_images/3110861-8ecc02bab563240f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图4.png"><br><img src="http://upload-images.jianshu.io/upload_images/3110861-09645a21c0fdad6b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="图5.png"></p>
<p><strong>运行时解析</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">1. 新建注解</span><br><span class="line">@Documented</span><br><span class="line">@Target(ElementType.METHOD)</span><br><span class="line">@Inherited</span><br><span class="line">@Retention(RetentionPolicy.RUNTIME)</span><br><span class="line">public @interface AuthorAnno &#123;</span><br><span class="line">	String name();</span><br><span class="line">	String website() default &quot;mazaiting&quot;;</span><br><span class="line">	int version() default 1;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">2. 创建主类，主类中</span><br><span class="line">public class AuthorAnnoDemo &#123;</span><br><span class="line">	@AuthorAnno(name=&quot;mazaiting&quot;,website=&quot;zaitingma@foxmail.com&quot;,version=1)</span><br><span class="line">	public static void main(String[] args) throws SecurityException, ClassNotFoundException &#123;</span><br><span class="line">		// 使用反射来解析注解</span><br><span class="line">		Parse.parse(AuthorAnnoDemo.class);</span><br><span class="line">		System.out.println(&quot;main&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	@AuthorAnno(name = &quot;zaitingma&quot;)</span><br><span class="line">	public void demo() &#123;</span><br><span class="line">		System.out.println(&quot;demo&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">3. 编写Parse.parse(Class&lt;AuthorAnnoDemo&gt; clazz) 方法来解析注解</span><br><span class="line">public class Parse &#123;</span><br><span class="line"></span><br><span class="line">	public static void parse(Class&lt;AuthorAnnoDemo&gt; clazz) throws SecurityException, ClassNotFoundException &#123;</span><br><span class="line">		// 获取类中的所有方法</span><br><span class="line">		Method[] methods = clazz.getMethods();		</span><br><span class="line">		// 遍历方法</span><br><span class="line">		for (Method method : methods)&#123;</span><br><span class="line">			// 如果该方法的注解父类是AuthorAnno</span><br><span class="line">			if (method.isAnnotationPresent(AuthorAnno.class)) &#123;</span><br><span class="line">				// 获取注解</span><br><span class="line">				AuthorAnno authorInfo = method.getAnnotation(AuthorAnno.class);</span><br><span class="line">				// 打印注解信息</span><br><span class="line">				System.out.println(&quot;method: &quot;+ method);</span><br><span class="line">                System.out.println(&quot;name= &quot;+ authorInfo.name() +</span><br><span class="line">                        &quot; , website= &quot;+ authorInfo.website()</span><br><span class="line">                       + &quot; , revision= &quot;+authorInfo.version());</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><a href="http://git.oschina.net/zaitingma/codes/3d9jiw8x1kbrmgq0tneha81" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/JakartaEE-Framework/JavaEE-压力测试工具/" rel="next" title="JavaEE-压力测试工具">
                <i class="fa fa-chevron-left"></i> JavaEE-压力测试工具
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Java/Java-Eclipse配置gradle编译项目/" rel="prev" title="Java-Eclipse配置gradle编译项目">
                Java-Eclipse配置gradle编译项目 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
